//
//  BaseSectionedTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/16.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class BaseSectionedTableViewController: UITableViewController {

    var sectiontitles: Array<String> = []
    
    var source: Array<Array<Dictionary<String,String?>>> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.backgroundColor = UIColor.dftBack
        self.tableView.register(UINib.init(nibName: "ApprovalDetailCell", bundle: Bundle.main), forCellReuseIdentifier: "ApprovalDetailCell")
        self.tableView.tableFooterView = UIView()
        self.tableView.reloadData()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return source.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if self.sectiontitles.count > section {
            return source[section].count + 1
        } else {
            return source[section].count
        }
    }//ok
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.sectiontitles.count > indexPath.section {
            if indexPath.row == 0 {
                if let cell = tableView.dequeueReusableCell(withIdentifier: "SectionTitle") {
                    return cell
                } else {
                    let cell = UITableViewCell.init(style: .default, reuseIdentifier: "SectionTitle")
                    cell.textLabel?.textColor = UIColor.mainColor
                    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
                    return cell
                }
            }
        }
        return tableView.dequeueReusableCell(withIdentifier: "ApprovalDetailCell")!
    }//ok
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if self.sectiontitles.count > indexPath.section {
            if indexPath.row == 0 {
                if sectiontitles.count > indexPath.section {
                    cell.textLabel?.text = sectiontitles[indexPath.section]
                }
                cell.selectionStyle = .none
            } else {
                if let cel = cell as? ApprovalDetailCell {
                    cel.selectionStyle = .none
                    if source.count > indexPath.section {
                        if source[indexPath.section].count + 1 > indexPath.row {
                            if indexPath.row > 0 {
                                let model = source[indexPath.section][indexPath.row - 1]
                                cel.title = model.keys.first ?? ""
                                cel.content = model[model.keys.first!] ?? ""
                            }
                        }
                    }
                }
            }
        } else {
            if let cel = cell as? ApprovalDetailCell {
                cel.selectionStyle = .none
                if source.count > indexPath.section {
                    if source[indexPath.section].count > indexPath.row {
                        let model = source[indexPath.section][indexPath.row]
                        cel.title = model.keys.first ?? ""
                        cel.content = model[model.keys.first!] ?? ""
                    }
                }
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.sectiontitles.count > indexPath.section {
            if source.count > indexPath.section {
                let array = source[indexPath.section]
                if array.count > indexPath.row {
                    if indexPath.row > 0 {
                        let model = array[indexPath.row - 1]
                        if let key = model.keys.first {
                            if let string = model[key] as? String {
                                if string.count > 0 {
                                    let size = string.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 40, height: 1000000))
                                    return 55 + size.height
                                }
                                return 65
                            }
                        }
                    }
                }
            }
            return 65
        } else {
            let array = source[indexPath.section]
            if array.count > indexPath.row {
                let model = source[indexPath.section][indexPath.row]
                if let key = model.keys.first {
                    if let string = model[key] as? String {
                        if string.count > 0 {
                            let size = string.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 40, height: 1000000))
                            return 55 + size.height
                        }
                        return 65
                    }
                }
            }
        }
        return 65
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.dftBack
        return view
    }

}
